﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ePlanet.SingleWell.Services;
using DreamBuilder.Core.Container;
using ePlanet.SingleWell.Models;
using System.Text;

namespace Web.Views.SingleWellManage
{
    /// <summary>
    /// SingleWellLazyTreeHandler 的摘要说明
    /// </summary>
    public class SingleWellLazyTreeHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            string parentId = context.Request.Params["id"];
            if (!string.IsNullOrEmpty(parentId))
            {
                context.Response.ContentType = "text/json";
                context.Response.Write(GetArchiveMenuData(parentId));
            }
            else
            {
                string param = context.Request.Params["param"];
                context.Response.ContentType = "text/json";
                context.Response.Write(getArchiveMenuDataByParam(param));
            }
            
            
        }

        public string GetArchiveMenuData(string parentId)
        {
            IWellCatalogService oIWellCatalogService = UContainer.Resolve<IWellCatalogService>();
            WellCatalog.WELLCATALOGDataTable oWellCatalog = oIWellCatalogService.GetWellCatalogByParentID(parentId).WELLCATALOG;
            string jsonFormat = "{{\"id\": \"{0}\", \"text\": \"{1}\",\"pid\": \"{2}\", \"iconCls\": \"{3}\",  \"state\": \"{4}\",\"attributes\":{{\"typeCode\": \"{5}\"}}}},";
            StringBuilder resultStr = new StringBuilder("[");
            string iconCss = string.Empty;
            string ParentID = string.Empty;
            string nodeState = "closed";
            int i = 1;
            foreach (WellCatalog.WELLCATALOGRow row in oWellCatalog.Rows)
            {
                if (string.IsNullOrEmpty(row.PARENTID.ToString()))
                {
                    ParentID = "0";
                }
                else
                {
                    ParentID = row.PARENTID.ToString();
                }
                iconCss = ParentID == "-1" ? "icon-type" : "icon-folder";
                //if (!row.PARENTID.Equals("-1") && !row.PARENTID.Equals("0")) nodeState = "open";
                if (row.ISWELLCODE == 0) nodeState = "open";
                else
                    nodeState = "closed";
                //iconCss = ParentID == "-1" ? "icon-close" : ParentID == "0" ? "icon-close" : "button ico_docu";
                resultStr.Append(string.Format(jsonFormat, row.WELLCATALOGID, row.CATALOGDESCRIPT, row.PARENTID, iconCss, nodeState, row.CATELOGSORT));
                i++;
            }
            if (resultStr.Length > 1)
            {
                resultStr = resultStr.Remove(resultStr.Length - 1, 1);
            }
            resultStr.Append("]");
            return resultStr.ToString();
        }

        public string getArchiveMenuDataByParam(string param)
        {
            string msg = "";
            int code=1;
            IWellCatalogService oIWellCatalogService = UContainer.Resolve<IWellCatalogService>();
            WellCatalog.WELLCATALOGDataTable oWellCatalog = oIWellCatalogService.GetWellCataLogByParam(param).WELLCATALOG;
            string jsonFormat = "{{\"id\": \"{0}\", \"text\": \"{1}\",\"pid\": \"{2}\", \"iconCls\": \"{3}\",  \"state\": \"{4}\",\"attributes\":{{\"typeCode\": \"{5}\"}}}},";
            StringBuilder resultStr = new StringBuilder("[");
            string iconCss = string.Empty;
            string ParentID = string.Empty;
            string nodeState = "open";
            int i = 1;
            //大于2000个节点给出提示
            if (oWellCatalog.Rows.Count > 3000)
            {
                msg = "您查询的节点数量为:" + oWellCatalog.Rows.Count + ",展示会非常缓慢.请您精确匹配查询条件";
                code = 0;
            }
            if (oWellCatalog.Rows.Count == 0)
            {
                oWellCatalog = oIWellCatalogService.GetWellCatalogByParentID("-1").WELLCATALOG;
                nodeState = "closed";
            }
            foreach (WellCatalog.WELLCATALOGRow row in oWellCatalog.Rows)
            {
                if (string.IsNullOrEmpty(row.PARENTID.ToString()))
                {
                    ParentID = "0";
                }
                else
                {
                    ParentID = row.PARENTID.ToString();
                }
                //if (row.ISWELLCODE.ToString().Equals("1"))
                //{
                //    nodeState = "closed";
                //}
                iconCss = ParentID == "-1" ? "icon-type" : "icon-folder";
                resultStr.Append(string.Format(jsonFormat, row.WELLCATALOGID, row.CATALOGDESCRIPT, row.PARENTID, iconCss, nodeState, row.CATELOGSORT));
                i++;
            }
            if (resultStr.Length > 1)
            {
                resultStr = resultStr.Remove(resultStr.Length - 1, 1);
            }
            resultStr.Append("]");
            return resultStr.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}